<?php
/*
 * @Descripttion: 
 * @version: 
 * @Author: Eugene
 * @Date: 2023-06-20 19:55:03
 * @LastEditors 肖宇 953675853@qq.com
 * @LastEditTime 2024-10-23 15:35:06
 */

namespace app\admin\controller\tj;

use app\common\controller\Backend;
use app\common\model\Custom;
use app\common\model\shop\Order;
use app\common\model\shop\Ordergoods;
use app\common\model\User;
use app\common\model\user\Commission;
use think\Db;

/**
 * 平台统计
 *
 * @icon fa fa-circle-o
 */
class Platform extends Backend
{

    /**
     * Order模型对象
     * @var \app\common\model\shop\Order
     */
    protected $model = null;
    protected $acctLimit = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\shop\Order;
        $this->view->assign("admin", $this->auth->getUserInfo());
    }



    /**
     * 查看
     */
    public function index()
    {
        // $this->view->assign("acctList", Category::column('id,name'));

        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {
        }
        // $column = [];
        // $starttime = Date::unixtime('day', -6);
        // $endtime = Date::unixtime('day', 0, 'end');
        // $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
        //     ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
        //     ->group('join_date')
        //     ->select();
        // for ($time = $starttime; $time <= $endtime;) {
        //     $column[] = date("Y-m-d", $time);
        //     $time += 86400;
        // }
        // $userlist = array_fill_keys($column, 0);
        // foreach ($joinlist as $k => $v) {
        //     $userlist[$v['join_date']] = $v['nums'];
        // }

        // $dbTableList = Db::query("SHOW TABLE STATUS");
        // $addonList = get_addon_list();
        // $totalworkingaddon = 0;
        // $totaladdon = count($addonList);
        // foreach ($addonList as $index => $item) {
        //     if ($item['state']) {
        //         $totalworkingaddon += 1;
        //     }
        // }
        $where = [];
        $whereCreatetime = [];
        $wherePaytime = [];

        if ($this->request->param('acct_id')) {
            $where['acct_id'] = $this->request->param('acct_id');
    
        }elseif($this->auth->acct_id){
            $where['acct_id'] = $this->auth->acct_id;
        }

        if ($this->request->param('time_range')) {
            // var_dump($filter);die;

            $arr = explode(' - ', $this->request->param('time_range'));
            $whereCreatetime['createtime'] = ['between', [strtotime($arr[0]), strtotime($arr[1] . ' 23:59:59')]];
            $wherePaytime['pay_time'] = ['between', [strtotime($arr[0]), strtotime($arr[1] . ' 23:59:59')]];
            
        }
        $users = User::where($where)->where($whereCreatetime)->count();
        $custom = Custom::where($where)->where($whereCreatetime)->count();
        $sale = Order::where($where)->where($wherePaytime)->where('status', '>=', '0.5')->sum('total_fee');

        $original_price = Order::where($where)->where($wherePaytime)->where('status', '>=', '0.5')->sum('original_price');
        
        $csale = Order::where($where)->where($wherePaytime)->where('status', '>=', '0.5')->count();
        $commission = Commission::where($where)->where($whereCreatetime)->sum('money');
        $this->view->assign($this->request->param());

        $this->view->assign([
            'users' => $users,
            'custom' => $custom,
            'sale' => $sale,
            'csale' => $csale,
            'profit' => round($sale - $original_price, 2),
            'commission' => $commission
        ]);

        // $this->view->assign([
        //     'toweek' => Order::where($where)->whereTime('pay_time', 'week')->sum('pay_fee'),
        //     'today' => Order::where($where)->whereTime('pay_time', 'today')->sum('pay_fee'),
        //     'lastday' => Order::where($where)->whereTime('pay_time', 'yesterday')->sum('pay_fee'),
        //     'tomonth' => Order::where($where)->whereTime('pay_time', 'month')->sum('pay_fee'),
        // ]);

        return $this->view->fetch();
    }
}
